var orderStatus = ['<div class="con-top"><p>订单已取消</p></div>',
                   '<div class="con-top"><p>待付款</p><span>赶紧付款吧~</span></div>',
                   '<div class="con-top"><p>已付款</p><span>请耐心等待宝贝~</span></div>',
                   '<div class="con-top"><p>已发货</p><span>宝贝正在飞奔向你~</span></div>',
                   '<div class="con-top"><p>已签收</p><span>赶快去评价吧~</span></div>',
                   '<div class="con-top"><p>已评价</p><span>期待您的下次购买哦~</span></div>'];
var param = JSON.parse(localStorage.getItem('param'));
var orderId = param.orderId;
var productId;
$(function(){
	init();
	initButton();
});
//初始化页面
function init(){
	$.ajax({
		type : "get",
		url : server_url
				+ 'rest/tea/order/getOrderInfo.do?ran=' + Math.random(),
		dataType : 'json',
		async : true,
		data : {
			token : token,
			orderId:orderId
		},
		success : function(result) {
			//console.log(result);
			var orderType ;
			if (result.retCode == '0000') {
				var order = result.data;
				orderType = order.type;
				$('#payPrice').html(order.totalunit);
				var product = order.teaProduct;
				productId = product.productid;
				var addr = order.teaUserAddr;
				var str = '<div class="order-top">'+orderStatus[parseInt(order.status)]+'</div>';
				if(order.wayInfo){
					console.log(order.wayInfo);
					var way = JSON.parse(order.wayInfo);
					//way.result.list[0].status = way.result.list[0].status.substring(0,22);
					str +='<div class="now-address"><p class="name">'+way.result.list[0].status+'</p>';
					if(way.result.list[0].status.length>=28){
						str +='<p class="way-con"></p>';
						str = str.replace(/now-address/,'way');
					}
					str +='<p class="now-con">'+way.result.list[0].time+'</p></div>';
				}
				str +='<div class="now-address"><p class="name">收件人：'+addr.userName+'<span>'+addr.userPhone+'</span></p><p class="now-con">'+addr.userArea+' '+addr.userAddr+'</p></div><div class="split-line"></div>';
				str +='<div class="pro-item"><div class="shop-box"><span>德盛茶叶行</span></div><div class="pro-box"><div class="pro-list clear"><img src="'+product.pic+'" alt="" class="fl"><div class="skill-box fr"><p class="skill-name">'+product.productname+'</p><p class="num clear">¥<span>'+product.priceretailguide+'<span><span class="fr">×'+order.conut+'</span></p></div></div></div><div class="num-box clear"><span class="fl">运费</span><span class="all-num fr">￥<span>'+product.freight+'</span></span></div><div class="num-box clear"><span class="fl">订单总价</span><div class="buy-num fr">￥<span>'+order.totalunit+'</span></div></div></div>';
				str +='<div class="order-box"><p>订单编号：'+order.orderno+'</p><p>创建时间：'+order.addtime+'</p>';
				if(order.paytime){
					str +='<p>付款时间：'+order.paytime+'</p>';
				}
				str +='</div>';
				$('#order').html(str);
				//判断是否需要显示取消订单
				if(order.status=='1'){//待付款
					var payStr = '<div class="total-box"><div class="go-shop iphonebtn">去付款</div><span class="cancle-order iphonebtn">取消订单</span></div>';
					$('#pay').html(payStr);
				}
				if(order.status=='3'){//发货后显示确认收货
					var payStr = '<div class="total-box"><div class="go-receive iphonebtn">确认收货</div></div>';
					$('#pay').html(payStr);
				}
				if(order.status=='4'){//确认收货后显示评价
					var payStr = '<div class="total-box"><div class="go-command iphonebtn">评论</div></div>';
					$('#pay').html(payStr);
				}
			} 
			
			$.ajax({
				type : "get",
				url : server_url
						+ 'rest/tea/product/getLikeProduct.do?ran=' + Math.random(),
				dataType : 'json',
				async : true,
				data : {
					token : token,
					type:orderType
				},
				success : function(result) {
					//console.log(result);
					if (result.retCode == '0000') {
						var str = '';
						var proList = result.data;
						if(proList.length>=1){
							str += '<div class="recommend-box"><div class="recommend-tit">猜你喜欢</div></div><div class="pro-list-b clear">';
							$.each(proList,function(i,n){
								str +='<div class="fl pro-item-b" id="'+n.productid+'" type="'+n.cateid+'"><img src="'+n.pic+'" alt=""><div class="name-box clear"><p class="fr normap">'+n.productname+'</p></div><div class="num-box clear"><div class="fl">￥<span>'+n.priceretailguide+'</span></div><p class="fr">已售'+n.teaSellNum+'件</p></div></div>';
							});
							str +="</div>";
						}
						$('#like').html(str);
					} else{
						showErrorMsg(result.data);
					}
				}
			});
		}
	});
	
}
//初始化绑定事件
function initButton(){
	//付款
	$('body').on('click','.go-shop',function(){
		$('.mask, .buy-pop').css({
			'display': 'block'
		})
	});
	//评价
	$('body').on('click','.go-command',function(){
		var param = {};
		param.productId = productId;
		param.orderId = orderId;
		//跳转
		localStorage.setItem('param',JSON.stringify(param));
		window.location=server_url+"orderComment.do";
	});
	//确认收货
	$('body').on('click','.go-receive',function(){
		$.ajax({
			type : "get",
			url : server_url
					+ 'rest/tea/order/makeSure.do?ran=' + Math.random(),
			dataType : 'json',
			async : true,
			data : {
				token : token,
				orderId:orderId
			},
			success : function(result) {
				console.log(result);
				if (result.retCode == '0000') {
					alert("确认收货");
					setTimeout(fresh,2000);
				}else{
					showErrorMsg(result.data);
				}
			}
		});
	});
	//取消订单
	$('body').on('click','.cancle-order',function(){
		$.ajax({
			type : "get",
			url : server_url
					+ 'rest/tea/order/cancleOrder.do?ran=' + Math.random(),
			dataType : 'json',
			async : true,
			data : {
				token : token
			},
			success : function(result) {
				//console.log(result);
				if (result.retCode == '0000') {
					alert("取消订单成功");
					window.location = server_url + "buyer/product/index.do";
				}else{
					showErrorMsg(result.data);
				} 
			}
		});
	});
	//猜你喜欢绑定
	$('body').on('click','.pro-item-b',function(){
		var productId = $(this).attr('id');
		var type = $(this).attr('type');
		var param = {};
		jumpDetail(type,param,productId);
	})
	//立即付款
	$('.buy-btn-b').on('click',function(){
		var payStyle ;
		if($('.own-pay').children('.chose-sel').prev().hasClass('buy-style1')){
			payStyle = '0';
		}else if($('.own-pay').children('.chose-sel').prev().hasClass('buy-style2')){
			payStyle = '1';
		}
		if(payStyle=='0'){
			$.ajax({
				type : "get",
				url : server_url
						+ 'rest/tea/pay/checkBalancePay.do?ran=' + Math.random(),
				dataType : 'json',
				async : true,
				data : {
					token : token,
					orderId:orderId,
					userId:localStorage.getItem('userid')
				},
				success : function(result) {
					console.log(result);
					if (result.retCode == '0000') {
						if(result.data){
							//检查成功
							//输入密码
							var yanzhengma = window.prompt("输入登录密码", "");
							$.ajax({
								type : "get",
								url : server_url
										+ 'rest/tea/pay/checkPayPassword.do?ran=' + Math.random(),
								dataType : 'json',
								async : true,
								data : {
									token : token,
									payPassword:yanzhengma,
									userId:localStorage.getItem('userid')
								},
								success : function(result) {
									if (result.retCode == '0000') {
										if(result.data){
											$.ajax({
												type : "get",
												url : server_url
														+ 'rest/tea/pay/balancePay.do?ran=' + Math.random(),
												dataType : 'json',
												async : true,
												data : {
													token : token,
													orderId:orderId,
													userId:localStorage.getItem('userid')
												},
												success : function(result) {
													if (result.retCode == '0000') {
														if(result.data){
															//支付成功，提示支付成功，然后跳转首页
															alert("支付成功");
															setTimeout(fresh,2000);
														}else{
															alert("支付失败");
														}
													}else{
														showErrorMsg(result.data);
													} 
												}
											});
										}else{
											alert("密码不正确");
										}
									}else{
										showErrorMsg(result.data);
									} 
								}
							});
						}else{
							alert("余额不足");
						}
					}else{
						showErrorMsg(result.data);
					} 
				}
			});
		}else{
			//微信支付
			window.location = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxb0f5fe89c5840bf4&redirect_uri=http://www.nashannacha.com/LianniuWeb/tea/views/pay/PayTest.html&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
		}
		/*if($('.own-pay').children('.chose-style').hasClass('chose-sel')) {
			var yanzhengma = window.prompt("输入登录密码", "")
			if(yanzhengma == 123) {
				alert("ok");
			} else {
				alert("false");
			}
		}*/
	});
}
function fresh(){
	window.location = server_url + "order.do";
}